package com.gexiaobao.pigeon.ui.fragment.mine.tools.query

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.ext.initClose
import com.gexiaobao.pigeon.app.ext.initFooter
import com.gexiaobao.pigeon.app.ext.loadListData
import com.gexiaobao.pigeon.app.ext.loadServiceInit
import com.gexiaobao.pigeon.app.ext.showLoading
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.app.weight.recycleview.SpaceItemDecoration
import com.gexiaobao.pigeon.databinding.FragmentRingRaceDetailBinding
import com.gexiaobao.pigeon.ui.adapter.SearchRaceListAdapter
import com.gexiaobao.pigeon.viewmodel.MineToolsViwModel
import com.kingja.loadsir.core.LoadService
import me.hgj.jetpackmvvm.ext.nav

/**
 * @Author      : hxw
 * @Date        : 2023/8/4 17:15
 * @Describe    : 该赛鸽的比赛详情
 */
class FragmentRingRaceDetail : BaseFragment<MineToolsViwModel, FragmentRingRaceDetailBinding>() {

    private var ringId: String = ""
    private val mAdapter: SearchRaceListAdapter by lazy { SearchRaceListAdapter(arrayListOf()) }
    private lateinit var loadsir: LoadService<Any>

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.includeBar.toolbar.initClose("详情") {
            nav().navigateUp()
        }

        /** 初始化adapter */
        mDatabind.layoutList.swipeRecyclerView.init(LinearLayoutManager(context), mAdapter).let {
            it.addItemDecoration(SpaceItemDecoration(0, Util.dp2px(context, 3f)))
            it.initFooter {
                /** 触发加载更多时请求数据 */
                mViewModel.searchRace(false, ringId)
            }
        }

        /** 状态页配置 */
        loadsir = loadServiceInit(mDatabind.layoutList.swipeRefreshLayout) {
            /** 点击重试时触发的操作 */
            onRefresh(true)
        }

        mDatabind.layoutList.swipeRefreshLayout.init {
            onRefresh(true)
        }
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
        ringId = arguments?.getString("ringId").toString()
        onRefresh(true)
    }

    private fun onRefresh(isRefresh: Boolean) {
        loadsir.showLoading()
        mViewModel.searchRace(isRefresh, ringId)
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.searchRaceResult.observe(viewLifecycleOwner) {
            loadListData(it, mAdapter, loadsir, mDatabind.layoutList.swipeRecyclerView, mDatabind.layoutList.swipeRefreshLayout)
        }
    }
}